package com.xing.mail;

import com.rabbitmq.client.Channel;
import com.xing.server.pojo.Employee;
import com.xing.server.pojo.MailConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.IOException;
import java.util.Date;

/**
 * 消息接收者
 *
 * @author: xingfuhao
 * @create: 2021-12-31 14:16
 */
@Component
public class MailReceiver {

    private static final Logger LOGGER= LoggerFactory.getLogger(MailReceiver.class);

    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private MailProperties mailProperties;
    @Resource
    private TemplateEngine templateEngine;
    @Autowired
    private RedisTemplate redisTemplate;


//  之前的
//    @RabbitListener(queues = "mail.welcome")
//    public void handler(Employee employee){
    @RabbitListener(queues = MailConstants.MAIL_QUEUE_NAME)
    public void handler(Message message, Channel channel){
        Employee employee = (Employee) message.getPayload();
        MessageHeaders headers = message.getHeaders();
        //消息序号
        long tag = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        String msgId = (String) headers.get("spring_returned_message_correlation");




        HashOperations redisHashOperations = redisTemplate.opsForHash();


        try {
            if(redisHashOperations.entries("mail_log").containsKey(msgId)){
                LOGGER.error("消息已经被消费====>{}",msgId);
                /**
                 * 手动确认消息
                 * tag:消息序号
                 * multiple:是否确认多条
                 */
                channel.basicAck(tag,false);
                return;
            }
            MimeMessage msg = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(msg);
            //发件人
            mimeMessageHelper.setFrom(mailProperties.getUsername());
            //收件人
            mimeMessageHelper.setTo(employee.getEmail());
            //主题
            mimeMessageHelper.setSubject("邢福豪");
            //发送日期
            mimeMessageHelper.setSentDate(new Date());
/**
*  //邮件内容
*             Context context = new Context();
*             context.setVariable("name", employee.getName());
*             context.setVariable("posName", employee.getPosition().getName());
*             context.setVariable("joblevelName", employee.getJoblevel().getName());
*             context.setVariable("departmentName", employee.getDepartment().getName());
*             //将数据导入模板
*             String mail = templateEngine.process("mail", context);
*             //设置发送文本
*             helper.setText(mail,true);
*             //发送
*             javaMailSender.send(msg);
*/

            //邮件内容
            Context context = new Context();
            context.setVariable("name", employee.getName());
            context.setVariable("posName", employee.getPosition().getName());
            context.setVariable("joblevelName", employee.getJoblevel().getName());
            context.setVariable("departmentName", employee.getDepartment().getName());
            //将数据导入模版
            String mail = templateEngine.process("mail", context);
            //设置发送文本
            mimeMessageHelper.setText(mail,true);
            //发送邮件
            javaMailSender.send(msg);
            LOGGER.info("邮件发送成功");
            //将消息id存入redis
            redisHashOperations.put("mail_log", msgId, "ok");
            //手动确认消息
            channel.basicAck(tag, false);
        } catch (Exception e) {
            /**
             * 手动确认消息
             * tag:消息序号
             * multiple:是否确认多条
             * requeue:是否退回到队列中
             */

            try {
                channel.basicNack(tag, false,true);
            } catch (IOException ioException) {
                ioException.printStackTrace();
                LOGGER.error("邮件发送失败========>{}",e.getMessage());
            }
            e.printStackTrace();
            LOGGER.error("邮件发送失败========>{}",e.getMessage());














        }


    }

}
